package com.krain.ddbb.activity;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.krain.corelibrary.helper.imageloader.ImageLoader;
import com.krain.corelibrary.widget.loadingView.LoadingView;
import com.krain.corelibrary.widget.recyleview.RefreshRecyclerView;
import com.krain.corelibrary.widget.recyleview.implement.OnRecyclerItemListener;
import com.krain.ddbb.R;
import com.krain.ddbb.adapter.AccountDetailAdapter;
import com.krain.ddbb.adapter.ServiceRemarkAdapter;
import com.krain.ddbb.base.BaseActivity;
import com.krain.ddbb.entity.AccountDetailBean;
import com.krain.ddbb.entity.AppApi;
import com.krain.ddbb.entity.ServiceRmarkBean;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.List;

/**
 * Created by ex-kuangxiaomin101 on 2015-12-28.
 */
@EActivity(R.layout.activity_account_detail)
public class AccountDetailActivity extends BaseActivity implements RefreshRecyclerView.SwipeRefreshListener {

    @ViewById(R.id.loadView)
    LoadingView mLoadingView;
    @ViewById(R.id.act_account_detail_recyleview)
    RefreshRecyclerView mRefreshRecyclerView;
    int maxPage;

    private List<AccountDetailBean> mEntityList;
    int pageIndex = 0;

    @AfterViews
    void afterView() {
        mRefreshRecyclerView.showRefreshing(true);
        mRefreshRecyclerView.setIsLoadMore(false);
        mRefreshRecyclerView.setSwipeRefreshListener(this);
        getAccountList(mEntityList, mRefreshRecyclerView, mLoadingView);
    }

    @Background
    protected void getAccountList(List<AccountDetailBean> mEntityList, RefreshRecyclerView mRefreshRecyclerView, LoadingView loadingView) {
        mEntityList = AppApi.getsInstance(mContext).getAccountList(app.getmUserinfo().getAccess_token(), pageIndex).getList();
        maxPage = AppApi.getsInstance(mContext).getAccountList(app.getmUserinfo().getAccess_token(), pageIndex).getPage();
        if (mEntityList != null) {
            if (mEntityList.size() == 0) {
                baseUtil.showSnackBar(this, "暂无数据");
            } else
                setAdapter(mEntityList, mRefreshRecyclerView, mLoadingView);
        }
    }
    AccountDetailAdapter adapter;
    @UiThread
    protected void setAdapter(List<AccountDetailBean> mEntityList, RefreshRecyclerView mRefreshRecyclerView, LoadingView loadingView) {
        mRefreshRecyclerView.setRefreshing(false);
        if (adapter == null){
            if (mEntityList.size()>10)   mRefreshRecyclerView.setIsLoadMore(true);
            adapter = new AccountDetailAdapter(mEntityList, mContext);
            mRefreshRecyclerView.setAdapter(adapter);
            adapter.setOnItemListener(new MyRecyclerItemListener(mEntityList));
            loadingView.showContentView(mRefreshRecyclerView);
        }else {
            if (maxPage == pageIndex){
                mRefreshRecyclerView.setIsLoadMore(false);
            }
            List<AccountDetailBean> list = adapter.getList();
            list.addAll(mEntityList);
            adapter.reflush(list);
            mRefreshRecyclerView.complete();
        }

    }

    @Override
    public void onLoadMore() {
        if (!mRefreshRecyclerView.getCanLoadMore()) return;
        pageIndex++;
        getAccountList(mEntityList, mRefreshRecyclerView, mLoadingView);
    }

    @Override
    public void onRefresh() {
        pageIndex = 0;
        adapter = null;
        getAccountList(mEntityList, mRefreshRecyclerView, mLoadingView);
    }

    /**
     * 列表的点击事件
     */

    class MyRecyclerItemListener extends OnRecyclerItemListener {
        List<AccountDetailBean> mEntityList;

        public MyRecyclerItemListener(List<AccountDetailBean> mEntityList) {
            this.mEntityList = mEntityList;
        }

        @Override
        public void onItemClick(int position, int viewType, RecyclerView.ViewHolder holder, View v) {

        }
    }


    public static void jumpTothisActivity(BaseActivity activity) {
        Intent intent = new Intent(activity, AccountDetailActivity_.class);
        activity.startActivity(intent);
    }

    @Override
    protected String getToolbarTitle() {
        return "收入明细";
    }
}
